﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Dtl;
using System.Data;


namespace Dal
{
    public class OrderInvoiceDao
    {

        public static DataSet getOInvoiceReport(string distributor, DateTime startDate, DateTime endDate, string ambient)       
        {
            string connection = Credentials.getCredentias(ambient);
            if (connection == null)
            {
                return null;
            }


            //distributor.Split('|').GetValue(X) : para sacar parametro idDIstribuidor y Modo de ejecucion del Procedimiento almacenado
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(connection);           
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "dbo.DMS_Report_ImportOrderInvoice";
            cmd.Parameters.AddWithValue("@idDistributor", distributor.Split('|').GetValue(0));
            cmd.Parameters.AddWithValue("@dtInvoiceStart", startDate);
            cmd.Parameters.AddWithValue("@dtInvoiceEnd", endDate);
            cmd.Parameters.AddWithValue("@modo", distributor.Split('|').GetValue(1));


            cmd.CommandTimeout = 0;           

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable("dtOInvoiceReport");

            try
            {                                       
                    cmd.Connection.Open();
                    da.Fill(dt);
                    cmd.Connection.Close();                
            }
            catch (Exception ex)
            {
                Credentials.writeLogFile("getOInvoiceReport: " + ex.Message);
                return null;
            }
            DataSet ds = new DataSet("dsOInvoiceReport");
            ds.Tables.Add(dt);
            return ds;
        }

        /*reporte de errores Orders e Invoices*/
        public static DataSet getOrderInvoice_AUD_Report(string distributor, string opcionReport_OI, DateTime startDate, DateTime endDate, string ambient)
        {

            string connection = Credentials.getCredentias(ambient);
            if (connection == null)
            {
                return null;
            }
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(connection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "dbo.dms_Report_ImportOrderInvoice_AUD";
            cmd.Parameters.AddWithValue("@Distri", distributor);
            cmd.Parameters.AddWithValue("@opcion", opcionReport_OI);
            cmd.Parameters.AddWithValue("@pifecha", startDate);
            cmd.Parameters.AddWithValue("@pffecha", endDate);
            cmd.CommandTimeout = 0;
                       
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable("dtImportOrderInvoice_AUD");

            try
            {                
                    cmd.Connection.Open();
                    da.Fill(dt);
                    cmd.Connection.Close();               
            }
            catch (Exception ex)
            {
                Credentials.writeLogFile("getOrderInvoice_AUD_Report: " + ex.Message);
                return null;
            }
            DataSet ds = new DataSet("dsImportOrderInvoice_AUD");
            ds.Tables.Add(dt);
            return ds;           
        }

    }
}
